package com.yww.leetcode2.linkedlist;

import com.yww.leetcode.common.ListNode;

/**
 * @author yww
 * @description 142. 环形链表 II
 * @since 2024/11/12
 */
public class DetectCycle142_06 {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                ListNode index = head;
                while (index != slow){
                    index = index.next;
                    slow = slow.next;
                }
                return index;
            }
        }
        return null;
    }
}
